package com.czk.jdbctemplate.dao;

import com.czk.jdbctemplate.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/11/23 21:03
 */
@Repository
public class DeptDap {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void add(Department department) {
        String sql = "insert into department value(?,?)";
        int i = jdbcTemplate.update(sql, department.getId(), department.getDepartmentName());
        System.out.println(i);
    }

    public int selectCount() {
        String sql = "select count(*) from department";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(integer);
        return integer;
    }

    /**
     * 返回对象
     *
     * @param id
     * @return
     */
    public Department findOne(String id) {
        String sql = "select * from department where id = ?";
        Department department = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Department>(Department.class), id);
        return department;
    }

    /**
     * 返回集合
     *
     * @return
     */
    public List<Department> findAll() {
        String sql = "select * from department ";
        List<Department> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Department.class));
        return query;
    }

    /**
     * 批量添加
     *
     * @param list
     */
    public void batchAdd(List<Object[]> list) {
        String sql = "insert into department value(?,?)";
        int[] ints1 = jdbcTemplate.batchUpdate(sql, list);
        System.out.println(ints1);
    }

    /**
     * 批量修改
     *
     * @param list
     */
    public void batchUpdate(List<Object[]> list) {
        String sql = "update department set departmentName=? where id =?";
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.asList(ints));

    }

    /**
     * 批量删除
     *
     * @param list
     */
    public void batchDelete(List<Object[]> list) {
        String sql = "delete from department where id =?";
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.asList(ints));

    }
}
